package com.yiren.backstage.db.dao;

import java.util.List;
import java.util.ArrayList;
import android.database.Cursor;
import android.database.sqlite.SQLiteStatement;

import org.greenrobot.greendao.AbstractDao;
import org.greenrobot.greendao.Property;
import org.greenrobot.greendao.internal.SqlUtils;
import org.greenrobot.greendao.internal.DaoConfig;
import org.greenrobot.greendao.database.Database;
import org.greenrobot.greendao.database.DatabaseStatement;

import com.yiren.backstage.currency.entity.ProjectEntity;

import com.yiren.backstage.currency.entity.GradeStandardEntity;

// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
/** 
 * DAO for table "grade_standard".
*/
public class GradeStandardEntityDao extends AbstractDao<GradeStandardEntity, String> {

    public static final String TABLENAME = "grade_standard";

    /**
     * Properties of entity GradeStandardEntity.<br/>
     * Can be used for QueryBuilder and for referencing column names.
     */
    public static class Properties {
        public final static Property Id = new Property(0, String.class, "id", true, "ID");
        public final static Property StandardContent = new Property(1, String.class, "standardContent", false, "STANDARD_CONTENT");
        public final static Property StandardVoiceName = new Property(2, String.class, "standardVoiceName", false, "STANDARD_VOICE_NAME");
        public final static Property AbbreviationContent = new Property(3, String.class, "abbreviationContent", false, "ABBREVIATION_CONTENT");
        public final static Property AbbreviationVoiceName = new Property(4, String.class, "abbreviationVoiceName", false, "ABBREVIATION_VOICE_NAME");
        public final static Property Grade = new Property(5, int.class, "grade", false, "GRADE");
        public final static Property Code = new Property(6, String.class, "code", false, "CODE");
        public final static Property ProjectId = new Property(7, String.class, "projectId", false, "PROJECT_ID");
        public final static Property ArtificialJudge = new Property(8, boolean.class, "artificialJudge", false, "ARTIFICIAL_JUDGE");
    }

    private DaoSession daoSession;


    public GradeStandardEntityDao(DaoConfig config) {
        super(config);
    }
    
    public GradeStandardEntityDao(DaoConfig config, DaoSession daoSession) {
        super(config, daoSession);
        this.daoSession = daoSession;
    }

    @Override
    protected final void bindValues(DatabaseStatement stmt, GradeStandardEntity entity) {
        stmt.clearBindings();
 
        String id = entity.getId();
        if (id != null) {
            stmt.bindString(1, id);
        }
 
        String standardContent = entity.getStandardContent();
        if (standardContent != null) {
            stmt.bindString(2, standardContent);
        }
 
        String standardVoiceName = entity.getStandardVoiceName();
        if (standardVoiceName != null) {
            stmt.bindString(3, standardVoiceName);
        }
 
        String abbreviationContent = entity.getAbbreviationContent();
        if (abbreviationContent != null) {
            stmt.bindString(4, abbreviationContent);
        }
 
        String abbreviationVoiceName = entity.getAbbreviationVoiceName();
        if (abbreviationVoiceName != null) {
            stmt.bindString(5, abbreviationVoiceName);
        }
        stmt.bindLong(6, entity.getGrade());
 
        String code = entity.getCode();
        if (code != null) {
            stmt.bindString(7, code);
        }
 
        String projectId = entity.getProjectId();
        if (projectId != null) {
            stmt.bindString(8, projectId);
        }
        stmt.bindLong(9, entity.getArtificialJudge() ? 1L: 0L);
    }

    @Override
    protected final void bindValues(SQLiteStatement stmt, GradeStandardEntity entity) {
        stmt.clearBindings();
 
        String id = entity.getId();
        if (id != null) {
            stmt.bindString(1, id);
        }
 
        String standardContent = entity.getStandardContent();
        if (standardContent != null) {
            stmt.bindString(2, standardContent);
        }
 
        String standardVoiceName = entity.getStandardVoiceName();
        if (standardVoiceName != null) {
            stmt.bindString(3, standardVoiceName);
        }
 
        String abbreviationContent = entity.getAbbreviationContent();
        if (abbreviationContent != null) {
            stmt.bindString(4, abbreviationContent);
        }
 
        String abbreviationVoiceName = entity.getAbbreviationVoiceName();
        if (abbreviationVoiceName != null) {
            stmt.bindString(5, abbreviationVoiceName);
        }
        stmt.bindLong(6, entity.getGrade());
 
        String code = entity.getCode();
        if (code != null) {
            stmt.bindString(7, code);
        }
 
        String projectId = entity.getProjectId();
        if (projectId != null) {
            stmt.bindString(8, projectId);
        }
        stmt.bindLong(9, entity.getArtificialJudge() ? 1L: 0L);
    }

    @Override
    protected final void attachEntity(GradeStandardEntity entity) {
        super.attachEntity(entity);
        entity.__setDaoSession(daoSession);
    }

    @Override
    public String readKey(Cursor cursor, int offset) {
        return cursor.isNull(offset + 0) ? null : cursor.getString(offset + 0);
    }    

    @Override
    public GradeStandardEntity readEntity(Cursor cursor, int offset) {
        GradeStandardEntity entity = new GradeStandardEntity( //
            cursor.isNull(offset + 0) ? null : cursor.getString(offset + 0), // id
            cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1), // standardContent
            cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2), // standardVoiceName
            cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3), // abbreviationContent
            cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4), // abbreviationVoiceName
            cursor.getInt(offset + 5), // grade
            cursor.isNull(offset + 6) ? null : cursor.getString(offset + 6), // code
            cursor.isNull(offset + 7) ? null : cursor.getString(offset + 7), // projectId
            cursor.getShort(offset + 8) != 0 // artificialJudge
        );
        return entity;
    }
     
    @Override
    public void readEntity(Cursor cursor, GradeStandardEntity entity, int offset) {
        entity.setId(cursor.isNull(offset + 0) ? null : cursor.getString(offset + 0));
        entity.setStandardContent(cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1));
        entity.setStandardVoiceName(cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2));
        entity.setAbbreviationContent(cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3));
        entity.setAbbreviationVoiceName(cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4));
        entity.setGrade(cursor.getInt(offset + 5));
        entity.setCode(cursor.isNull(offset + 6) ? null : cursor.getString(offset + 6));
        entity.setProjectId(cursor.isNull(offset + 7) ? null : cursor.getString(offset + 7));
        entity.setArtificialJudge(cursor.getShort(offset + 8) != 0);
     }
    
    @Override
    protected final String updateKeyAfterInsert(GradeStandardEntity entity, long rowId) {
        return entity.getId();
    }
    
    @Override
    public String getKey(GradeStandardEntity entity) {
        if(entity != null) {
            return entity.getId();
        } else {
            return null;
        }
    }

    @Override
    public boolean hasKey(GradeStandardEntity entity) {
        return entity.getId() != null;
    }

    @Override
    protected final boolean isEntityUpdateable() {
        return true;
    }
    
    private String selectDeep;

    protected String getSelectDeep() {
        if (selectDeep == null) {
            StringBuilder builder = new StringBuilder("SELECT ");
            SqlUtils.appendColumns(builder, "T", getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T0", daoSession.getProjectEntityDao().getAllColumns());
            builder.append(" FROM grade_standard T");
            builder.append(" LEFT JOIN project T0 ON T.\"PROJECT_ID\"=T0.\"ID\"");
            builder.append(' ');
            selectDeep = builder.toString();
        }
        return selectDeep;
    }
    
    protected GradeStandardEntity loadCurrentDeep(Cursor cursor, boolean lock) {
        GradeStandardEntity entity = loadCurrent(cursor, 0, lock);
        int offset = getAllColumns().length;

        ProjectEntity projectEntity = loadCurrentOther(daoSession.getProjectEntityDao(), cursor, offset);
        entity.setProjectEntity(projectEntity);

        return entity;    
    }

    public GradeStandardEntity loadDeep(Long key) {
        assertSinglePk();
        if (key == null) {
            return null;
        }

        StringBuilder builder = new StringBuilder(getSelectDeep());
        builder.append("WHERE ");
        SqlUtils.appendColumnsEqValue(builder, "T", getPkColumns());
        String sql = builder.toString();
        
        String[] keyArray = new String[] { key.toString() };
        Cursor cursor = db.rawQuery(sql, keyArray);
        
        try {
            boolean available = cursor.moveToFirst();
            if (!available) {
                return null;
            } else if (!cursor.isLast()) {
                throw new IllegalStateException("Expected unique result, but count was " + cursor.getCount());
            }
            return loadCurrentDeep(cursor, true);
        } finally {
            cursor.close();
        }
    }
    
    /** Reads all available rows from the given cursor and returns a list of new ImageTO objects. */
    public List<GradeStandardEntity> loadAllDeepFromCursor(Cursor cursor) {
        int count = cursor.getCount();
        List<GradeStandardEntity> list = new ArrayList<GradeStandardEntity>(count);
        
        if (cursor.moveToFirst()) {
            if (identityScope != null) {
                identityScope.lock();
                identityScope.reserveRoom(count);
            }
            try {
                do {
                    list.add(loadCurrentDeep(cursor, false));
                } while (cursor.moveToNext());
            } finally {
                if (identityScope != null) {
                    identityScope.unlock();
                }
            }
        }
        return list;
    }
    
    protected List<GradeStandardEntity> loadDeepAllAndCloseCursor(Cursor cursor) {
        try {
            return loadAllDeepFromCursor(cursor);
        } finally {
            cursor.close();
        }
    }
    

    /** A raw-style query where you can pass any WHERE clause and arguments. */
    public List<GradeStandardEntity> queryDeep(String where, String... selectionArg) {
        Cursor cursor = db.rawQuery(getSelectDeep() + where, selectionArg);
        return loadDeepAllAndCloseCursor(cursor);
    }
 
}
